package com.gc.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gc.business.domain.ElevatorFault;
import com.gc.business.domain.ElevatorRepair;
import com.gc.business.mapper.ElevatorRepairMapper;
import com.gc.business.service.ICommunityManageService;
import com.gc.business.service.IElevatorInfoService;
import com.gc.business.service.IElevatorRepairService;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.bean.BeanUtils;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysDictDataService;
import com.gc.system.service.ISysUserService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 电梯维修 服务层实现
 * 
 * @author liujinteng
 * @date 2020-11-20
 */
@Slf4j
@Service
public class ElevatorRepairServiceImpl extends ServiceImpl<ElevatorRepairMapper, ElevatorRepair> implements IElevatorRepairService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IElevatorInfoService elevatorInfoService;

    @Autowired
    private ICommunityManageService communityManageService;

    @Override
    public List<ElevatorRepair> list(ElevatorRepair elevatorRepair,List<Long> collect){
        LambdaQueryWrapper<ElevatorRepair> wrapper = new LambdaQueryWrapper<>();
        if(elevatorRepair != null){
            if(StringUtils.isNotEmpty(elevatorRepair.getRepairId())){
                wrapper.eq(ElevatorRepair::getRepairId, elevatorRepair.getRepairId());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getElevatorName())){
                wrapper.like(ElevatorRepair::getElevatorName, elevatorRepair.getElevatorName());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getRepairCode())){
                wrapper.eq(ElevatorRepair::getRepairCode, elevatorRepair.getRepairCode());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getFaultCode())){
                wrapper.eq(ElevatorRepair::getFaultCode, elevatorRepair.getFaultCode());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getStartDate())){
                wrapper.eq(ElevatorRepair::getStartDate, elevatorRepair.getStartDate());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getIsStopElevator())){
                wrapper.eq(ElevatorRepair::getIsStopElevator, elevatorRepair.getIsStopElevator());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getSubmitDate())){
                wrapper.eq(ElevatorRepair::getSubmitDate, elevatorRepair.getSubmitDate());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getRepairHours())){
                wrapper.eq(ElevatorRepair::getRepairHours, elevatorRepair.getRepairHours());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getReplaceParts())){
                wrapper.eq(ElevatorRepair::getReplaceParts, elevatorRepair.getReplaceParts());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getFaultReason())){
                wrapper.like(ElevatorRepair::getFaultReason, elevatorRepair.getFaultReason());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getRepairPlan())){
                wrapper.eq(ElevatorRepair::getRepairPlan, elevatorRepair.getRepairPlan());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getRepairPeople())){
                wrapper.eq(ElevatorRepair::getRepairPeople, elevatorRepair.getRepairPeople());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getRepairImage())){
                wrapper.eq(ElevatorRepair::getRepairImage, elevatorRepair.getRepairImage());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getRepairSign())){
                wrapper.eq(ElevatorRepair::getRepairSign, elevatorRepair.getRepairSign());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getDoneStatus())){
                wrapper.eq(ElevatorRepair::getDoneStatus, elevatorRepair.getDoneStatus());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getConfirmPeople())){
                wrapper.eq(ElevatorRepair::getConfirmPeople, elevatorRepair.getConfirmPeople());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getConfirmDate())){
                wrapper.eq(ElevatorRepair::getConfirmDate, elevatorRepair.getConfirmDate());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getConfirmSign())){
                wrapper.eq(ElevatorRepair::getConfirmSign, elevatorRepair.getConfirmSign());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getScore())){
                wrapper.eq(ElevatorRepair::getScore, elevatorRepair.getScore());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getScoreContent())){
                wrapper.like(ElevatorRepair::getScoreContent, elevatorRepair.getScoreContent());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getCreateBy())){
                wrapper.eq(ElevatorRepair::getCreateBy, elevatorRepair.getCreateBy());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getCreateTime())){
                wrapper.eq(ElevatorRepair::getCreateTime, elevatorRepair.getCreateTime());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getUpdateBy())){
                wrapper.eq(ElevatorRepair::getUpdateBy, elevatorRepair.getUpdateBy());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getUpdateTime())){
                wrapper.eq(ElevatorRepair::getUpdateTime, elevatorRepair.getUpdateTime());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getRemark())){
                wrapper.eq(ElevatorRepair::getRemark, elevatorRepair.getRemark());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getDispatchPeople())){
                wrapper.eq(ElevatorRepair::getDispatchPeople, elevatorRepair.getDispatchPeople());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getDispatchDate())){
                wrapper.eq(ElevatorRepair::getDispatchDate, elevatorRepair.getDispatchDate());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getReportType())){
                wrapper.eq(ElevatorRepair::getReportType, elevatorRepair.getReportType());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getSubmitter())){
                wrapper.eq(ElevatorRepair::getSubmitter, elevatorRepair.getSubmitter());
            }
            if(StringUtils.isNotEmpty(elevatorRepair.getElevatorId())){
                wrapper.eq(ElevatorRepair::getElevatorId, elevatorRepair.getElevatorId());
            }
            //社区挂钩
            if (CollUtil.isNotEmpty(collect)){
                wrapper.in(ElevatorRepair::getElevatorId,collect);
            }
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据故障单保存维修单
     * @param elevatorFault
     * @return
     */
    @Override
    public int saveElevatorRepairByFault(ElevatorFault elevatorFault){
        ElevatorRepair elevatorRepair = new ElevatorRepair();
        BeanUtils.copyBeanProp(elevatorRepair,elevatorFault);
        //维修单号
        elevatorRepair.setRepairCode("WX".concat(String.valueOf(IdUtils.getPrimaryKey())));
        //故障原因
        String business_fault_type = dictDataService.selectDictDataByType("business_fault_type", elevatorFault.getFaultType());
        elevatorRepair.setFaultReason(business_fault_type);
        elevatorRepair.setCreateTime(DateUtil.date());
        return baseMapper.insert(elevatorRepair);
    }


    /**
     * 分审核状态查询维修列表 -- 根据不同人物权限
     * @param userId
     * @param reviewType
     * @param isOverhaul 是否大修(0:是;1:否)
     * @return
     */
    @Override
    public List<ElevatorRepair> selectListByStatus(Long userId ,Long reviewType,String isOverhaul,Integer pageNum,Integer pageSize){
        List<ElevatorRepair> elevatorRepairList = new ArrayList<>();
        SysUser sysUser = sysUserService.selectUserById(userId);
        //查询用户角色列
        String userRoles = sysUserService.selectUserRole(userId);

        //判断是否为维修经理
        if(reviewType == 2L && userRoles.contains(BusinessConstants.KEEP_MANAGER)){
            if(pageNum != null && pageSize != null){PageHelper.startPage(pageNum,pageSize,"er.dispatch_date DESC");}
            elevatorRepairList = baseMapper.selectListByStatus(reviewType,sysUser.getAttrId(),isOverhaul);
        }else{
            Console.log("reviewType:{},attrId:{},isOverhaul:{}",reviewType,sysUser.getAttrId(),isOverhaul);
            if(pageNum != null && pageSize != null){PageHelper.startPage(pageNum,pageSize,"er.dispatch_date DESC");}
            elevatorRepairList = baseMapper.selectListByStatus(reviewType,sysUser.getAttrId(),isOverhaul);
        }
        return elevatorRepairList;
    }

    @Override
    public List<ElevatorRepair> selectRepairList(ElevatorRepair elevatorRepair, Long reviewType, Long attrId, String isOverhaul) {
        return baseMapper.selectRepairList(elevatorRepair,reviewType,attrId,isOverhaul);
    }
}
